//
//  DCAlertView.m
//  Demo
//
//  Created by zhaolin on 2023/1/13.
//

#import "DCAlertView.h"



typedef NS_ENUM(NSUInteger, GradientType) {
    GradientTypeTopToBottom = 0,//从上到下
    GradientTypeLeftToRight = 1,//从左到右
};
@interface DCAlertView ()

@property (nonatomic, strong)  UIView *bgView;

@property (nonatomic, strong) UIImageView *logoImage;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UIButton *commitBtn;

@end

@implementation DCAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (void)configureTitle:(NSString *)title content:(NSString *)content commitTitle:(NSString *)commintTitle {
    self.titleLabel.text  = title;
    self.contentLabel.text = content;
    [self.commitBtn setTitle:commintTitle forState:UIControlStateNormal];
}
- (void)configureUI {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgAction:)];
    [self addGestureRecognizer:tap];
    
    self.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0.7];
    UIView *bgView = [UIView new];
    _bgView = bgView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor colorWithString:@"#FAFFEF"];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
//        make.height.mas_equalTo(306);
    }];
    
    UIImageView *logoImage = [UIImageView new];
    logoImage.image = [UIImage imageNamed:@"icon_alert_title"];
    _logoImage = logoImage;
    [bgView addSubview:logoImage];
    [logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(28);
        make.width.height.mas_equalTo(58);
        make.centerX.mas_equalTo(bgView);
    }];
    
    UILabel *titleLabel = [UILabel new];
    _titleLabel = titleLabel;
    titleLabel.text = @"title";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = DCFont_PF_Semibold(18);
    titleLabel.textColor = [UIColor colorWithString:@"#000000"];
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(logoImage.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
    
    UILabel *contentLabel = [UILabel new];
    _contentLabel = contentLabel;
    contentLabel.font = DCFont_PF_Regular(14);
    contentLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    contentLabel.text = @"#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B#1C221B";
    contentLabel.numberOfLines = 0;
    [bgView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(-35);
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(11);
    }];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commitBtn = commitBtn;
    commitBtn.layer.cornerRadius = 8;
    commitBtn.layer.masksToBounds = YES;
    commitBtn.backgroundColor = [UIColor colorWithString:@"#000000"];
    commitBtn.titleLabel.font = DCFont_PF_Semibold(15);
    [commitBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    [commitBtn setTitle:@"OK" forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitBtnAtion:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.height.mas_equalTo(42);
        make.top.mas_equalTo(contentLabel.mas_bottom).mas_offset(39);
    }];
    
    [bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(commitBtn.mas_bottom).mas_offset(30);
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self createByCAGradientLayer:[UIColor colorWithString:@"#FAFFEF"] endColor:[UIColor colorWithString:@"#FBFDF8"] layerFrame:_bgView.bounds direction:GradientTypeTopToBottom];
    [self.bgView bringSubviewToFront:_logoImage];
    [self.bgView bringSubviewToFront:_titleLabel];
    [self.bgView bringSubviewToFront:_contentLabel];
    [self.bgView bringSubviewToFront:_commitBtn];
}

- (void)commitBtnAtion:(UIButton *)sender {
    if (self.callback) {
        self.callback(@"1");
    }
}

- (void)tapBgAction:(UITapGestureRecognizer *)sender {
    if (self.callback) {
        self.callback(@"0");
    }
}

-(void)createByCAGradientLayer:(UIColor *)startColor endColor:(UIColor *)endColor layerFrame:(CGRect)frame direction:(GradientType)direction{
    CAGradientLayer *layer = [CAGradientLayer new];
    //存放渐变的颜色的数组
    layer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    //起点和终点表示的坐标系位置，(0,0)表示左上角，(1,1)表示右下角
    switch (direction) {
        case GradientTypeTopToBottom:
            layer.startPoint = CGPointMake(0.0, 0.0);
            layer.endPoint = CGPointMake(0.0, 1);
            break;
        case GradientTypeLeftToRight:
            layer.startPoint = CGPointMake(0.0, 0.0);
            layer.endPoint = CGPointMake(1, 0.0);
            break;
        default:
            break;
    }
    layer.frame = frame;
    [self.bgView.layer addSublayer:layer];
}

@end
